$(function() {
    //获取id
    var id = tools.queryString().id || 1;
    //页面加载时默认样式
    $($(".productMsgOrComments")[0]).addClass("showMsgOrComments")
    $($(".allCommentList")).addClass("showcommentList");
    //获取商品详情
    $.ajax({
        type: "get",
        url:domain.online + "/api/product",
        // url: domain.test + "/getproductdetail",
        data: {id: id},
        success: function(data) {
            // if(data.data.rtnCode === 200) {
            if(data.rtnCode === 200) {
                // var productDetail = data.data.data;
                var productDetail = data.data;
                //商品图片轮播图
                $(".swiper-container").html(template("productAutoImg", { pics: productDetail.pics }));
                var mySwiper = new Swiper('.swiper-container', {
                    autoplay: 3000,
                    pagination: ".swiper-pagination",
                    loop: true,
                    autoplayDisableOnInteraction: false
                });
                //商品信息
                document.title = productDetail.productName;
                $(".product_Name").html(template("productMsgAndPrice", { msg: productDetail }));
                //新欢商品
                $('.like').html(template("peopleLikeProduct", { love: productDetail.love }));
                //商品详情
                $(".productMsg").html(template("msgContent", { details: productDetail.details }));
                $("img.lazy").lazyload({
                    palceholder:"../../img/loading.gif",
                    effect: "fadeIn", 
                    threshold: 50,  
                    failurelimit : 10 
                });
            }
        }
    });
    //获取商品评论数
    $.ajax({
        type: "get",
        // url: domain.test + "/getproductcommentsnum",
        url:domain.online + "/api/product/review/number",
        data:{id:id},
        success:function(data) {
            // if(data.data.rtnCode === 200) {
            if(data.rtnCode === 200) {
                // var commentsNum = data.data.data;
                var commentsNum = data.data;
                $(".btn").html(template("btns",{commentsNum:commentsNum.hao+commentsNum.zhong+commentsNum.cha}));
                $($(".msgAndcommentsBtn")[0]).addClass("btnActive");
                $(".commentClassify").html(template("commentClassifyBtn",{allCommentNum:commentsNum}));
                $($(".commentItem")[0]).addClass("activeComments");
            }
        }
    });
    //获取所有商品评论
    $.ajax({
        type: "get",
        // url: domain.test + "/getproductallcomments",
        url: domain.online + "/api/product/review/list",
        data:{id:id},
        success:function(data) {
            // if(data.data.rtnCode === 200) {
            if(data.rtnCode === 200) {
                // var comments = data.data.data;
                var comments = data.data;
                $(".allCommentList").html(template("allCommentsTpl",{commenList:comments.list}));
                getGoodComments(id);
            }
        }
    });
    //获取商品好评列表
    function getGoodComments(id){
        $.ajax({
        type: "get",
        // url: domain.test + "/getproductgoodcomments",
        url: domain.online + "/api/product/review/list",
        data:{id:id,score:1,pageNum:1},
        success:function(data) {
            // if(data.data.rtnCode === 200) {
            if(data.rtnCode === 200) {
                // var comments = data.data.data;
                var comments = data.data;
                $(".goodCommentList").html(template("goodCommentsTpl",{commenList:comments.list}));
                getNeutralComments(id);
            }
        }
    });
    }
    //获取商品中评列表
    function getNeutralComments(id) {
        $.ajax({
        type: "get",
        // url: domain.test + "/getproductneutralcomments",
        url: domain.online + "/api/product/review/list",
        data:{id:id,score:0,pageNum:1},
        success:function(data) {
            // if(data.data.rtnCode === 200) {
            if(data.rtnCode === 200) {
                // var comments = data.data.data;
                var comments = data.data;
                $(".neutralCommentList").html(template("neutralCommentsTpl",{commenList:comments.list}));
                getBadComments(id);
            }
        }
    });
    }
    //获取商品差评列表
    function getBadComments(id) {
        $.ajax({
        type: "get",
        // url: domain.test + "/getproductbadcomments",
        url: domain.online + "/api/product/review/list",
        data:{id:id,score:-1,pageNum:1},
        success:function(data) {
            // if(data.data.rtnCode === 200) {
            if(data.rtnCode === 200) {
                // var comments = data.data.data;
                var comments = data.data;
                $(".badCommentList").html(template("badCommentsTpl",{commenList:comments.list}));
            }
        }
    });
    }
    //商品详情和评论的切换
    $(".btn").on("click",".msgAndcommentsBtn",function() {
        $(".msgAndcommentsBtn").removeClass("btnActive");
        $(this).addClass("btnActive");
        var index = $(this).attr("index");
        $(".productMsgOrComments").removeClass("showMsgOrComments");
        $($(".productMsgOrComments")[index - 1]).addClass("showMsgOrComments");
    });
    //商品评论的切换
    $(".commentClassify").on("click",".commentItem",function() {
        $(".commentItem").removeClass("activeComments");
        $(this).addClass("activeComments");
        var index = $(this).attr("index");
        $(".commentList").removeClass("showcommentList");
        $($(".commentList")[index - 1]).addClass("showcommentList");
    });
    //跳转下载app
    $(".footer").on("click", function() {
        if(navigator.userAgent.indexOf("iPhone") > -1) {
            location.href = "https://itunes.apple.com/cn/app/id1234070505";
        }else if(navigator.userAgent.indexOf("Android") > -1) {
            location.href = "http://a.app.qq.com/o/simple.jsp?pkgname=net.pusai.mall";
        }
    });
});